//
//  PPAlbumPictureInsertionPictureViewController.m
//  pickpic
//
//  Created by Jinxiao on 10/15/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentPictureViewController.h"

#import "PPAlbumPictureDescriptionViewController.h"

#import "PPAlbumPictureInsertionPictureActionView.h"

#import "PPAlbumPictureDescriptionView.h"

#import "PPPreViewBar.h"
@interface PPAlbumContentPictureViewController () <PPAlbumPictureDescriptionViewControllerDelegate>
@property (readwrite, nonatomic, strong) PPAlbumPictureDescriptionView *descriptionView;
@property (nonatomic,strong) PPPreViewBar *navBarView;
@end

@implementation PPAlbumContentPictureViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    self.navBarView.frame = CGRectMake(0, 0, SCREEN_WIDTH, kPPPreViewBarH);
    
}
- (PPPreViewBar *)navBarView{
    
    if (!_navBarView) {
        _navBarView = [PPPreViewBar preViewBar];
        _navBarView.backgroundColor = WHITE;
        [_navBarView.rightBtn setTitle:nil forState:UIControlStateNormal];
        [_navBarView.rightBtn setImage:[UIImage imageNamed:@"icon-home-more"] forState:UIControlStateNormal];
        _navBarView.rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 0);
        [_navBarView.rightBtn setTintColor:BLACK];
        WEAKSELF
        _navBarView.BlockDidClick = ^(NSInteger index) {
            if (index == 0) {
                [weakSelf popViewPreviousController];
            }else{
                [weakSelf moreItemDidPressed];
            }
        };
        
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 63.5,SCREEN_WIDTH, 0.5)];
        line.backgroundColor = COLORA(240,240, 240);
        [_navBarView addSubview:line];
        
    }
    return _navBarView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    [self.view addSubview:self.navBarView];
    
    //    [self setupBackItem];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-home-more"] style:UIBarButtonItemStylePlain target:self action:@selector(moreItemDidPressed)];
//    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];

    _descriptionView = [[PPAlbumPictureDescriptionView alloc] init];
    _descriptionView.backgroundColor = WHITE;
    _descriptionView.alpha = 0.9;
    [self.view addSubview:_descriptionView];

    [_descriptionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];

    [self updateRelevantInfos];
 
}
- (void)updateRelevantInfos {
    PPAbstractArtwork *artwork = [self.artworks safeObjectAtIndex:self.index];
    _descriptionView.desc = artwork.desc;
    _descriptionView.label.backgroundColor = WHITE;
    _descriptionView.label.textColor = BLACK;
}

- (void)moreItemDidPressed {
    [MobClick event:@"52"]; 
    PPAlbumPictureInsertionPictureActionView *actionView = [[NSBundle mainBundle] loadNibNamed:@"PPAlbumPictureInsertionPictureActionView" owner:nil options:nil].firstObject;
    actionView.frame = CGRectMake(0, self.navigationController.view.height - actionView.height, self.navigationController.view.width, actionView.height);

    DGPopoverView *popoverView = [[DGPopoverView alloc] initWithContainerView:self.navigationController.view contentView:actionView options:DGPopoverViewAnimationOptionUpward backgroundStyle:DGPopoverViewBackgroundStyleDimmed];

    __weak typeof(self)weakSelf = self;
    __weak typeof(popoverView)weakPopover = popoverView;
    [actionView.cancelButton setActionBlock:^(UIControl *control) {
        [weakPopover dismissAnimated:YES completion:nil];
    } forControlEvents:UIControlEventTouchUpInside];

    [actionView.coverButton setActionBlock:^(UIControl *control) {
        [weakPopover dismissAnimated:YES completion:^{
            [MobClick event:@"54"];
            PPAbstractArtwork *artwork = [weakSelf.artworks objectAtIndex:weakSelf.index];
            [weakSelf.delegate pictureViewController:weakSelf setArtworkAsCover:artwork];

            [weakSelf.navigationController.view toastMessage:@"设置封面成功"];
        }];
    } forControlEvents:UIControlEventTouchUpInside];

    [actionView.descButton setActionBlock:^(UIControl *control) {
        [weakPopover dismissAnimated:YES completion:^{
            [MobClick event:@"53"];
            __strong typeof(weakSelf)strongSelf = weakSelf;

            if(strongSelf == nil) {
                return;
            }

            PPAbstractArtwork *artwork = [weakSelf.artworks safeObjectAtIndex:weakSelf.index];

            PPAlbumPictureDescriptionViewController *descriptionViewController = [[PPAlbumPictureDescriptionViewController alloc] init];
            descriptionViewController.delegate = strongSelf;
            descriptionViewController.artwork = artwork;
            [weakSelf.navigationController pushViewController:descriptionViewController animated:YES];

        }];
    } forControlEvents:UIControlEventTouchUpInside];

    [popoverView displayAnimated:YES completion:nil];
}

- (void)didEndScrolling {
    [super didEndScrolling];

    [self updateRelevantInfos];
}

#pragma mark - PPAlbumPictureDescriptionViewControllerDelegate

- (void)descriptionViewController:(PPAlbumPictureDescriptionViewController *)descriptionViewController didSaveDescription:(NSString *)description {
    PPAbstractArtwork *artwork = descriptionViewController.artwork;
    artwork.desc = description;

    [_delegate pictureViewController:self setDescription:description forArtwork:artwork];

    [self updateRelevantInfos];
}

@end
